import axios from 'axios'
import stores from '@/vuex/store'
// import router from "@/router";

import {Message} from 'element-ui'

// 发送请求之前
axios.interceptors.request.use((config) => {
  stores.commit('AJAXCOUNT', stores.getters.ajaxCount + 1)
  if (stores.getters.ajaxCount > 0) {
    stores.dispatch('showloader')
  }
  // if (config.method === "post") {
  //     config.data = qs.stringify(config.data);
  // }
  return config
}, (error) => {
  // eslint-disable-next-line
  return Promise.reject(error);
})

// 响应数据
axios.interceptors.response.use((response) => {
  stores.commit('AJAXCOUNT', stores.getters.ajaxCount - 1)
  if (stores.getters.ajaxCount === 0) {
    stores.dispatch('hideloader')
  }
  if (response.data.status === 0) {
    return response.data
  } else if (response.data.status === 1) {
    Message.warning(response.data.msg)
  } else {
    Message.error(response.data.msg)
  }
  return response
}, (error) => {
  Message.error('网络开小差！')
  stores.commit('AJAXCOUNT', 0)
  stores.dispatch('hideloader')
  // eslint-disable-next-line
  return Promise.reject(error);
})

export default axios
